// GridLayout.cc

/*
   Copyright 2006 Corey Tabaka

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#include <GridLayout.h>
#include <Container.h>

namespace gui {

GridLayout::GridLayout(void) {
	GridLayout(1, 0, 0, 0);
}

GridLayout::GridLayout(int rows, int cols) {
	GridLayout(rows, cols, 0, 0);
}

GridLayout::GridLayout(int rows, int cols, int hgap, int vgap) {
	this->rows = rows;
	this->cols = cols;
	this->hgap = hgap;
	this->vgap = vgap;
}

GridLayout::~GridLayout(void) {
}

int GridLayout::getRows(void) {
	return rows;
}

int GridLayout::getCols(void) {
	return cols;
}

void GridLayout::setRows(int rows) {
	this->rows = rows;
}

void GridLayout::setCols(int cols) {
	this->cols = cols;
}

Dimension GridLayout::getPreferredLayoutSize(RefContainer parent) {
	Insets insets = parent->getInsets();
	int ncomponents = parent->getComponentCount();
	int nrows = rows;
	int ncols = cols;
	
	if (nrows > 0) {
		ncols = (ncomponents + nrows - 1) / nrows;
	} else {
		nrows = (ncomponents + ncols - 1) / ncols;
	}
	
	int w = 0;
	int h = 0;
	for (int i = 0 ; i < ncomponents ; i++) {
		RefComponent comp = parent->getComponent(i);
		Dimension d = comp->getPreferredSize();
		
		if (w < d.width) {
			w = d.width;
		}
		
		if (h < d.height) {
			h = d.height;
		}
	}
	
	Dimension dim(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
		insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
	
	return dim;
}

Dimension GridLayout::getMinimumLayoutSize(RefContainer parent) {
	Insets insets = parent->getInsets();
	int ncomponents = parent->getComponentCount();
	int nrows = rows;
	int ncols = cols;
	
	if (nrows > 0) {
		ncols = (ncomponents + nrows - 1) / nrows;
	} else {
		nrows = (ncomponents + ncols - 1) / ncols;
	}
	
	int w = 0;
	int h = 0;
	for (int i = 0 ; i < ncomponents ; i++) {
		RefComponent comp = parent->getComponent(i);
		Dimension d = comp->getMinimumSize();
		
		if (w < d.width) {
			w = d.width;
		}
		
		if (h < d.height) {
			h = d.height;
		}
	}
	
	Dimension dim(insets.left + insets.right + ncols*w + (ncols-1)*hgap, 
		insets.top + insets.bottom + nrows*h + (nrows-1)*vgap);
	
	return dim;
}

void GridLayout::layoutContainer(RefContainer parent) {
	Insets insets = parent->getInsets();
	int ncomponents = parent->getComponentCount();
	int nrows = rows;
	int ncols = cols;
	bool ltr = true; //parent.getComponentOrientation().isLeftToRight();
	
	if (ncomponents == 0) {
		return;
	}
	
	if (nrows > 0) {
		ncols = (ncomponents + nrows - 1) / nrows;
	} else {
		nrows = (ncomponents + ncols - 1) / ncols;
	}
	
	int w = parent->getWidth() - (insets.left + insets.right);
	int h = parent->getHeight() - (insets.top + insets.bottom);
	w = (w - (ncols - 1) * hgap) / ncols;
	h = (h - (nrows - 1) * vgap) / nrows;
	
	if (ltr) {
		for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {
			for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
				int i = r * ncols + c;
				
				if (i < ncomponents) {
					parent->getComponent(i)->setBounds(x, y, w, h);
				}
			}
		}
	} else {
		for (int c = 0, x = parent->getWidth() - insets.right - w; c < ncols ; c++, x -= w + hgap) {
			for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {
				int i = r * ncols + c;
				
				if (i < ncomponents) {
					parent->getComponent(i)->setBounds(x, y, w, h);
				}
			}
		}
	}
}

} // namespace gui
